package com.team.RunsFast_Api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team.RunsFast_Api.dao.ScoreLogDao;
import com.team.RunsFast_Api.dao.UserSignDao;
import com.team.RunsFast_Api.service.intf.UserLevelService;
import com.team.RunsFast_Api.service.intf.UserSignService;
import com.team.RunsFast_Api.util.DateUtil;
import com.team.runsfast.entity.ScoreLog;
import com.team.runsfast.entity.UserSign;
import com.team.runsfast.vo.R;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class UserSignServiceImpl implements UserSignService {

    private final UserSignDao dao;
    private final ScoreLogDao scoreLogDao;
    private final UserLevelService userLevelService;

    public UserSignServiceImpl(UserSignDao dao, ScoreLogDao scoreLogDao, UserLevelService userLevelService) {
        this.dao = dao;
        this.scoreLogDao = scoreLogDao;
        this.userLevelService = userLevelService;
    }

    @Override
    public R queryAll() {
        return R.ok(dao.selectList(null));
    }

    //创建签到记录
    @Override
    public R createSign(String token) {
        long uid = Long.parseLong(token);

        //查询以往有没有签到记录
        UserSign usersign=dao.selectLast(uid);

        //签到记录
        UserSign us = new UserSign();
        us.setUid(uid);
        us.setCtime(new Date());
        us.setScore(5);
        us.setExscore(0);
        us.setDays(1);

        //首次签到
        if(usersign == null){
            us.setExscore(100);//首次奖励积分
            us.setInfo("首签好礼 + 100积分");
        }else{
            int day = DateUtil.diffDay(usersign.getCtime());
            if(day == 0){
                return R.fail("今日您已签到，请勿重复签到！");
            }else if(day == 1){
                us.setDays(usersign.getDays() + 1); //连续签到天数+1
                if(us.getDays() % 30 == 0){
                    us.setExscore(300);
                    us.setInfo("连续签到"+us.getDays()+"天，给予奖励100");
                }else if(us.getDays() % 7 == 0){
                    us.setExscore(70);
                    us.setInfo("连续签到"+us.getDays()+"天，给予奖励50");
                }
            }else{
                //断签
                //验证是否需要给予 回归奖励
                if(day > 365){
                    us.setExscore(3650);
                    us.setInfo("您已"+us.getDays()+"天没有签到，给予奖励365");
                }
            }
        }

        dao.insert(us);

        scoreLogDao.insert(new ScoreLog(uid,1,"签到奖励积分",us.getScore()+us.getExscore()));

        userLevelService.updateLevel(uid,us.getScore()+us.getExscore());

        return R.ok(us.getScore()+us.getExscore());
    }

    //判断今天能不能签到
    @Override
    public R checkSign(String token) {
        long uid = Long.parseLong(token);
        QueryWrapper<UserSign> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        UserSign us = dao.selectLast(uid);
        if(us == null){
            return R.ok();
        }else{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if(sdf.format(us.getCtime()).equals(sdf.format(new Date()))){
                    return R.fail("今天已签到");
            }else{
                    return R.ok();
            }
        }
    }
}
